from selenium import webdriver
from lxml import etree
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions as EC
import pandas as pd


productname='SJY560'   ##需要查询的产品
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-automation'])
options.add_argument("--disable-blink-features=AutomationControlled")
bro=webdriver.Chrome(options=options)


bro.get("https://www.simuwang.com/")  ##模拟打开到排排网主页

page_text=bro.page_source
tree=etree.HTML(page_text)
test=tree.xpath("//div[@class='posr comp-common-flex jcc aic']/span[1]")

login_tag1=bro.find_element(By.XPATH,".//div[@class='posr comp-common-flex jcc aic']/span[1]").click()

bro.implicitly_wait(5)
username_tag=bro.find_element_by_name("username")
password_tag=bro.find_element_by_xpath("/html/body/div[4]/div/div/div/div[2]/div/div[3]/form/div[2]/input")

username_tag.send_keys('13861815985')
password_tag.send_keys('123456y')   ##输入登录的账号密码

btn=bro.find_element_by_class_name("comp-login-btn")
btn.click()
sleep(3)   ##通过输入的账号密码登录






product_name=bro.find_element_by_xpath("/html/body/div[2]/div/div/div[1]/div[1]/div/div[2]/div[1]/div/input")
product_name.send_keys(productname)
btn=bro.find_element_by_class_name("comp-header-submit")
btn.click()   ##查询指定产品

text=bro.find_element_by_xpath("/html/body/div[2]/div/div/div[2]/div[2]/div[1]/div/div[2]/div[2]/p")
text.click()   ##进入产品主页

handles=bro.window_handles
bro.switch_to.window(handles[1])   ##跳转到新打开的页面


text=bro.find_element_by_xpath("/html/body/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/div[1]/div[1]/a[2]")
text.click()

sleep(2)


page_text1=bro.page_source
tree1=etree.HTML(page_text1)
r=tree1.xpath("/html/body/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/div[1]/div[3]/div[2]/div[*]/div[1]/text()")
old=len(r) #初始时下滑窗口捕捉到的数据量



##页面窗口滑动到底
while True:
    js='document.getElementsByClassName("tbody")[0].scrollTop=10000'
    bro.execute_script(js)
    sleep(1)
    page_text1=bro.page_source
    tree1=etree.HTML(page_text1)
    r=tree1.xpath("/html/body/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/div[1]/div[3]/div[2]/div[*]/div[1]/text()")
    new=len(r)
    print(new)
    if old==new:   ##判断下滑窗口数据量是否更新，如果没有更新，说明已经滑动到底
        break
    else:
        old=new




df=pd.DataFrame()
dict=dict()

#开始获取数据
page_text1=bro.page_source
tree1=etree.HTML(page_text1)

#获取日期
date=tree1.xpath("/html/body/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/div[1]/div[3]/div[2]/div[*]/div[1]/text()")
dict["date"]=date

#获取单位净值
per_value=[]
cum_value1=[]
cum_value2=[]
change=[]
for i in range(len(date)):
    path_per_value="/html/body/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/div[1]/div[3]/div[2]/div["+str(i+1)+"]/div[" \
                                                                                                              "2]/*[" \
                                                                                                              "contains(@class,'f02e') or contains(@class,'8883') or contains(@class,'d63c') or contains(@class,'a78a')]/text() "
    elements_per_value=tree1.xpath(path_per_value)
    elements_per_value=float("".join(elements_per_value))
    per_value.append(elements_per_value)

    path_cum_value1="/html/body/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/div[1]/div[3]/div[2]/div["+str(i+1)+"]/div[3]/*[contains(@class,'f02e') or contains(@class,'8883') or contains(@class,'d63c') or contains(@class,'a78a')]/text() "
    elements_sum_value1=tree1.xpath(path_cum_value1)
    elements_sum_value1=float("".join(elements_sum_value1))
    cum_value1.append(elements_sum_value1)

    path_cum_value2 = "/html/body/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/div[1]/div[3]/div[2]/div[" + str(
        i + 1) + "]/div[4]/*[contains(@class,'f02e') or contains(@class,'8883') or contains(@class,'d63c') or " \
                 "contains(@class,'a78a')]/text() "
    elements_sum_value2 = tree1.xpath(path_cum_value2)
    elements_sum_value2 = float("".join(elements_sum_value2))
    cum_value2.append(elements_sum_value2)

    path_change = "/html/body/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/div[1]/div[3]/div[2]/div[" + str(
        i + 1) + "]/div[5]/text()"
    elements_change=tree1.xpath(path_change)
    change+=elements_change


dict["单位净值"]=per_value
dict["累计净值(分红再投资)"]=cum_value1
dict["累计净值(分红不投资)"]=cum_value2
dict["变动"]=change
df=pd.DataFrame(dict)
print(df)

text=bro.find_element_by_xpath("/html/body/div[2]/div/div/div[2]/div[1]/div[1]/div/div[4]/div[7]/a").click()
sleep(3)
handles=bro.window_handles
bro.switch_to.window(handles[2])
page_text1=bro.page_source
tree1=etree.HTML(page_text1)
index=tree1.xpath("/html/body/div[2]/div/div/div[2]/div[2]/div[1]/div[1]/div[4]/div[3]/a/text()")
print(index)

df.to_excel(productname+str('.xls'),sheet_name='data',index=False)

